﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Fast.Framework.Interfaces;


namespace Fast.Framework
{

    /// <summary>
    /// 查询建造者实现类
    /// </summary>
    public class QueryBuilder : IQueryBuilder
    {

        /// <summary>
        /// 表名称
        /// </summary>
        public string TableName { get; set; }

        /// <summary>
        /// 不同
        /// </summary>
        public bool Distinct { get; set; }

        /// <summary>
        /// 列
        /// </summary>
        public string Columns { get; set; }

        /// <summary>
        /// 条件
        /// </summary>
        public List<string> Where { get; }

        /// <summary>
        /// 连接
        /// </summary>
        public List<string> Join { get; }

        /// <summary>
        /// 分组
        /// </summary>
        public string Group { get; set; }

        /// <summary>
        /// 有
        /// </summary>
        public List<string> Having { get; }

        /// <summary>
        /// 排序
        /// </summary>
        public List<string> Order { get; }

        /// <summary>
        /// 联合
        /// </summary>
        public List<string> Union { get; }

        /// <summary>
        /// 全联合
        /// </summary>
        public List<string> UnionAll { get; }

        /// <summary>
        /// 参数
        /// </summary>
        public Dictionary<string, object> Parameters { get; }

        /// <summary>
        /// 构造方法
        /// </summary>
        public QueryBuilder()
        {
            Where = new List<string>();
            Join = new List<string>();
            Having = new List<string>();
            Order = new List<string>();
            Union = new List<string>();
            UnionAll = new List<string>();
            Parameters = new Dictionary<string, object>();
        }

        /// <summary>
        /// 到字符串
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            var sb = new StringBuilder();
            sb.AppendLine("SELECT");
            if (Distinct)
            {
                sb.AppendLine("DISTINCT");//不同
            }
            sb.AppendLine(Columns);//列
            sb.AppendLine("FROM");
            if (Union.Count > 0 || UnionAll.Count > 0)//判断是否存在联合
            {
                sb.AppendLine("(");
                if (Union.Count > 0)
                {
                    sb.AppendLine(string.Join("\r\nUNION\r\n", Union));//联合
                }
                if (UnionAll.Count > 0)
                {
                    sb.AppendLine(string.Join("\r\nUNION ALL\r\n", UnionAll));//全联合
                }
                sb.AppendLine(")");
            }
            sb.AppendLine($"{TableName}");//表名称
            if (Join.Count > 0)
            {
                sb.AppendLine($"{string.Join("\r\n", Join)}");//连接 
            }
            if (Where.Count > 0)
            {
                sb.AppendLine($"WHERE {string.Join(" AND ", Where)}");//条件
            }
            if (!string.IsNullOrWhiteSpace(Group))//分组
            {
                sb.AppendLine($"GROUP BY {Group}");
                if (Having.Count > 0)//有
                {
                    sb.AppendLine($"HAVING {string.Join("\t", Having)}");
                }
            }
            if (Order.Count > 0)
            {
                sb.AppendLine($"ORDER BY {string.Join(",", Order)}");//排序
            }
            //Console.WriteLine(sb.ToString().Trim());
            return sb.ToString().Trim();
        }
    }
}
